package com.lemon.reggie.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lemon.reggie.common.R;
import com.lemon.reggie.pojo.dto.AgainOrderDTO;
import com.lemon.reggie.pojo.dto.GiveOrderDTO;
import com.lemon.reggie.pojo.dto.SubmitOrderDTO;
import com.lemon.reggie.pojo.vo.OrderPageVO;
import com.lemon.reggie.pojo.vo.OrderVO;
import com.lemon.reggie.service.OrdersService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * <p>
 * 订单表 前端控制器
 * </p>
 *
 * @author Lemon
 * @since 2022-05-23
 */
@RestController
@RequestMapping("/order")
@Validated
public class OrdersController {

    @Resource
    private OrdersService ordersService;

    @PostMapping("/submit")
    public R<?> submitOrder(@RequestBody @Valid SubmitOrderDTO submitOrderDTO) {
        ordersService.submitOrder(submitOrderDTO);
        return R.success("下单成功！");
    }

    @GetMapping("/userPage")
    public R<?> userOrderPage(@RequestParam("page") Long currentPage, @RequestParam("pageSize") Long pageSize) {
        IPage<OrderVO> iPage = ordersService.userOrderPage(currentPage, pageSize);
        return R.success(iPage);
    }

    @PostMapping("/again")
    public R<?> againOrder(@RequestBody @Valid AgainOrderDTO againOrderDTO) {
        ordersService.againOrder(againOrderDTO);
        return R.success("下单成功！");
    }

    @GetMapping("/page")
    public R<?> orderPage(@RequestParam("page") Long currentPage,
                          @RequestParam("pageSize") Long pageSize,
                          @RequestParam(value = "number",required = false) String number,
                          @RequestParam(value = "beginTime",required = false) String beginTime,
                          @RequestParam(value = "endTime",required = false) String endTime) {
        IPage<OrderPageVO> iPage = ordersService.orderPage(currentPage, pageSize, number,
                beginTime, endTime);
        return R.success(iPage);
    }

    @PutMapping
    public R<?> giveOrder(@RequestBody @Valid GiveOrderDTO giveOrderDTO) {
        ordersService.giveOrder(giveOrderDTO);
        return R.success("派送成功！");
    }

}

